package com.lovo.system.controller;

import com.lovo.system.dto.SaleDto;
import com.lovo.system.service.ISaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/sale")
public class SaleController {

    @Autowired
    ISaleService saleService;

    @RequestMapping("/info")
    public List<SaleDto> info(HttpServletRequest request){
        return saleService.info((Integer) request.getSession().getAttribute("userId"));
    }

    @RequestMapping("/sell")
    public String buyer(HttpServletRequest request){
        return (String) request.getSession().getAttribute("realName");
    }


    @RequestMapping("/add")
    public void addSale(@RequestBody SaleDto saleDto,HttpServletRequest request){
        saleDto.setUserId((Integer) request.getSession().getAttribute("userId"));
        if (saleService.selectRepeat(saleDto)==1){
            saleService.insertSale(saleDto);
        }
    }

    @RequestMapping("/compareNumber")
    public boolean compareNumber(int number,String name){
        return saleService.compareNumber(number,name);
    }

    @RequestMapping("/delete")
    public String deleteSale(int id){
        return saleService.deleteSale(id);
    }

    @RequestMapping("/changeNumber")
    public void changeNumber(@RequestBody SaleDto saleDto){
        saleService.updateSale(saleDto);
    }

    @RequestMapping("/deleteAll")
    public void deleteAll(HttpServletRequest request){
        saleService.deleteAll((Integer) request.getSession().getAttribute("userId"));
    }

}
